//
//  MainViewController.m
//  SmileRead
//
//  Created by dllo on 15/8/21.
//  Copyright (c) 2015年 JIZ. All rights reserved.
//

#import "MainViewController.h"
#import "MyViewOfScrollView.h"
#import "Path.h"
#import "JokesMainViewController.h"
#import "UnderViewController.h"
#import "VoiceMainViewController.h"
#import "ReadMainViewController.h"
#import "NoteMainViewController.h"
#import "Reachability.h"
#import "AppDelegate.h"


@interface MainViewController () <MyViewOfScrollViewDelegate>

// 用于展示的页面
@property (nonatomic, retain) UIScrollView *scroll;

// 背景图片
@property (nonatomic, retain) UIImageView *backImage;

// 毛玻璃
@property (nonatomic, retain) UIVisualEffectView *effectView;

// 轮播图封装
@property (nonatomic, retain) MyViewOfScrollView *itemView;

//轮播图图片的数组
@property (nonatomic, retain) NSMutableArray *array;

@property (nonatomic, retain) LeftSlideViewController *drawer;

@property (nonatomic, retain) UILabel *funtionLabel;

@property (nonatomic, retain) UILabel *titleOfLabel;


@end

@implementation MainViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = @[@"9.jpg", @"7.jpg", @"8.jpg", @"9.jpg", @"7.jpg"].mutableCopy;
    }
    return self;
}

- (void)dealloc
{
    [_titleOfLabel release];
    [_funtionLabel release];
    [_drawer release];
    [_array release];
    [_itemView release];
    [_effectView release];
    [_backImage release];
    [_scroll release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    // 添加动画
    [UIView animateKeyframesWithDuration:1 delay:1 options:UIViewKeyframeAnimationOptionRepeat|UIViewKeyframeAnimationOptionAutoreverse animations:^{
        self.titleOfLabel.alpha = 0.1;
    } completion:^(BOOL finished) {
        self.titleOfLabel.alpha = 1;
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"倾心";
    
    self.navigationController.navigationBar.barTintColor = [Path colorWithRed:200 green:209 blue:233];
    
    // 创建left按钮来控制抽屉
    UIButton *leftBut = [UIButton buttonWithType:UIButtonTypeCustom];
   
    [leftBut setBackgroundImage:[UIImage imageWithContentsOfFile:[Path pathOfImageWithName:@"10.png"]] forState:UIControlStateNormal];
    
    leftBut.frame = CGRectMake(0, 0, 30, 30);
    //leftBut.backgroundColor = [UIColor cyanColor];
    [leftBut setTitle:@"点击" forState:UIControlStateNormal];
    [leftBut addTarget:self action:@selector(clickedOfDrawer:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:leftBut] autorelease];;
    [self jugeNet];
    
    [self createSubView];
}

// 实现抽屉的控制
- (void)clickedOfDrawer:(id)sender
{
    if (self.drawer.closed) {
        [self.drawer openLeftView];
        UnderViewController *under = (UnderViewController *)self.drawer.leftVC;
        [under receiveWithDrawer:self.drawer];
    }
    else {
        [self.drawer closeLeftView];
    }
}

- (void)jugeNet
{
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    if ([reach isReachable]) {
        return;
    }
    else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"网络连接" message:@"现在没有网, 请检查网络连接." preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            
        }];
        
        [alert addAction:action];
        AppDelegate *tempAppDelegate = [[UIApplication sharedApplication] delegate];
        
        [tempAppDelegate.window.rootViewController presentViewController:alert animated:YES completion:nil];
    }
}



// 创建子视图
- (void)createSubView
{
    
    self.backImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.backImage.image = [UIImage imageWithContentsOfFile:[Path pathOfImageWithName:@"7.jpg"]];
    self.backImage.userInteractionEnabled = YES;
    
    // 在backImage上铺毛玻璃
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    self.effectView = [[UIVisualEffectView alloc] initWithEffect:blur];
    self.effectView.frame = CGRectMake(0, 0, self.backImage.frame.size.width, self.backImage.frame.size.height);
    [self.backImage addSubview:self.effectView];
    [self.view addSubview:self.backImage];
    
    self.titleOfLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 * widthFit, 64, self.effectView.frame.size.width - 10 * widthFit, 180 * heightFit - 64)];
    self.titleOfLabel.numberOfLines = 0;
    self.titleOfLabel.textAlignment = NSTextAlignmentCenter;
    self.titleOfLabel.textColor = [UIColor whiteColor];
    self.titleOfLabel.font = [UIFont systemFontOfSize:25];
    self.titleOfLabel.text = @"Our mind is a sponge, Our heart is a stream.";
    [self.effectView.contentView addSubview:self.titleOfLabel];
    
    self.itemView = [[MyViewOfScrollView alloc] initWithFrame:CGRectMake(0, 180 * heightFit, self.effectView.frame.size.width, 300 * heightFit) arrayOfImage:self.array];
    self.itemView.delegate = self;
    
    // target/action, 点击图片跳转页面
    self.itemView.target = self;
    self.itemView.action = @selector(ClickedOfImage:);
    [self.effectView addSubview:self.itemView];
    
    [_itemView release];
    [_backImage release];
    [_effectView release];
    
    [self createFooterSubView];
}

// 点击图片跳转页面
- (void)ClickedOfImage:(NSNumber *)number
{
    NSInteger index = [number integerValue];
    switch (index) {
        case 0: {
            // 跳转到段子页面
            JokesMainViewController *jokes = [[JokesMainViewController alloc] init];
            [jokes receiveBackImageName:self.array[index + 1]];
            [self.navigationController pushViewController:jokes animated:YES];
            [jokes release];
        }
            break;
        case 1: {
            VoiceMainViewController *voice = [[VoiceMainViewController alloc] init];
            [voice receiveBsckImageName:self.array[index +1]];
            [self.navigationController pushViewController:voice animated:YES];
            [voice release];
            
        }
            break;
            
        case 2: {
            
            ReadMainViewController *readCon = [[ReadMainViewController alloc] init];
            [self.navigationController pushViewController:readCon animated:YES];
            [readCon release];
        }
        default:
            break;
    } 
    // 0: 跳转Jokes, 1: 跳转Picture 2: 跳转Voice
}

// MyViewOfScrollViewDelegate 协议方法, 改变背景图片
- (void)sendWithIndex:(NSInteger)index
{
    NSString *path = [Path pathOfImageWithName:self.array[index]];
    self.backImage.image = [UIImage imageWithContentsOfFile:path];
}


#warning footer的设计还没完成
// 对于首页footer的设计
- (void)createFooterSubView
{
    UIButton *noteButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 80 * widthFit, 500 * heightFit, 160 * widthFit, 50 * heightFit)];
    noteButton.backgroundColor = [UIColor greenColor];
    noteButton.layer.cornerRadius = 10 * widthFit;
    [noteButton setTitle:@"心情日记" forState:UIControlStateNormal];
    [noteButton addTarget:self action:@selector(note) forControlEvents:UIControlEventTouchUpInside];
    [self.effectView.contentView addSubview:noteButton];
    
}

// 日记
- (void)note
{
    NoteMainViewController *add = [[NoteMainViewController alloc] init];
    
    [self.navigationController pushViewController:add animated:YES];
    [add release];
}


// 执行LeftSlideViewControllerDelegate代理方法
- (void)sendWithViewController:(LeftSlideViewController *)drawer
{
    // 接受抽屉
    self.drawer = drawer;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
